﻿using System;
using System.Security.Cryptography;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;

namespace Rally_MvvmC.Library
{
    public class Encryptor : CryptoEngine<TwofishEngine, Sha1Digest>
    {
        public Encryptor(Encoding encoding, string key, string macKey)
            : base(encoding, GetKey(key), GetKey(macKey))
        {
        }

        public Encryptor(string key, string macKey)
            : base(Encoding.UTF8, GetKey(key), GetKey(macKey))
        {
        }

        private static byte[] GetKey(string text)
        {
            if (text == null || text.Length < 256) return null;

            var str2 = (text.Substring(text.Length / 2) + text.Substring(0, text.Length / 2)).Substring(0,32);

            var len = str2.Length;
            // this was posted by petebob as well 
            char[] array = str2.ToCharArray();
            Array.Reverse(array);

            var str3 = new String(array);
            var len2 = str3.Length;

            return Encoding.UTF8.GetBytes(str3);
        }

    }
}